﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace quizzer_linq_3
{
    /**Class: Question_Lookup
     * @author Samuel McCravy
     * @version 1.0
     * Course : ITEC 4860 Spring 2012
     * Written: March 1, 2012
     * 
     * This Class is suppose to allow admins to look up questions.
     * 
     * Purpose: The purpose of this class is to look up questions and return them to the user.
    */

    public partial class Question_Lookup : Form
    {
        //This is used to lookup main ID's

        const int MAIN_ID_LOOKUP_INDEX = 0;

        //This is a constant used to lookup last name for professor

        const int PROFESSOR_EMAIL_LOOKUP_INDEX = 3;

        //This is a constant used to lookup the category and subcategory names

        const int CATEGORY_AND_SUBCATEGORY_NAME_LOOKUP_INDEX = 1;

        public Question_Lookup()
        {
            InitializeComponent();
            //Populates the combo boxes

            populateCategoryComboBox();
            populateProfessorComboBox();
        }

        /** Method: exitButton_Click
         * Closes this form
         * 
         * @param sender sender of the action
         * @param e event is triggered
        */

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /** Method: populateCategoryComboBox
         * Populates the category combo boxe with categories
         * 
        */

        private void populateCategoryComboBox()
        {
            //DataAccess is the variable for using the database

            Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

            //Gets all the categories into an array

            String[] getAllCategories = dataAccess.GetAllCategory().Split('\n');

            //Puts all the categories into the combo box

            for (int counter = 0; counter < getAllCategories.Length - 1; counter++)
            {
                String[] getCategory = ((String)(getAllCategories.GetValue(counter))).Split('|');
                lookupCategoryComboBox.Items.Add(getCategory.GetValue(CATEGORY_AND_SUBCATEGORY_NAME_LOOKUP_INDEX));
            }
        }

        /** Method: populateProfessorComboBox
        * Populates the professor combo boxe with categories
        * 
       */

        private void populateProfessorComboBox()
        {
            //DataAccess is the variable for using the database

            Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

            //Gets all the professors and puts them in an array

            String[] getAllProfessors = dataAccess.GetAllProfessor().Split('\n');

            //Puts all the professors into a combo box

            for (int counter = 0; counter < getAllProfessors.Length - 1; counter++)
            {
                String[] getProfessor = ((String)(getAllProfessors.GetValue(counter))).Split('|');
                lookupProfessorComboBox.Items.Add(getProfessor.GetValue(PROFESSOR_EMAIL_LOOKUP_INDEX));

            }
        }

        /** Method: lookupCategoryComboBox_SelectedIndexChanged
         * Populates the subcategory box with subcategories everytime the category box is changed
         *
         * @param sender sender of the action
         * @param e event is triggered
        */
        private void lookupCategoryComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Clears the combo box each time a new item is selected

            lookupSubCategoryComboBox.Items.Clear();

            //Gets the category that is selected in the combo box

            String category = (String)lookupCategoryComboBox.SelectedItem;

            //DataAccess is the variable for using the database

            Quizzer_Data_Access dataAccess = new Quizzer_Data_Access();

            //Gets the category ID so we can look up the sub category

            int categoryID = int.Parse(dataAccess.GetCategory(category.Trim()).Split('|').GetValue(MAIN_ID_LOOKUP_INDEX).ToString());

            //Gets all the sub-categories for selected category

            String[] getSubCategories = dataAccess.GetSubCategoryForCategory(categoryID).Split('\n');

            //Putting all the subcategories into the combo box

            for (int counter = 0; counter < getSubCategories.Length - 1; counter++)
            {
                String[] getSubCategory = ((String)(getSubCategories.GetValue(counter))).Split('|');
                lookupSubCategoryComboBox.Items.Add(getSubCategory.GetValue(CATEGORY_AND_SUBCATEGORY_NAME_LOOKUP_INDEX));
            }
        }
        /** Method: questionLookupButton_Click
         * Looks up the questions and presents them in a pop-up box.
         *
         * @param sender sender of the action
         * @param e event is triggered
        */
        private void questionLookupButton_Click(object sender, EventArgs e)
        {
            if (setIDRadioButton.Checked)
            {
                this.Hide();
                QuestionsAndAnswers QuestionAndAnswerForm = new QuestionsAndAnswers(int.Parse(textBoxSetID.Text.Trim()));
                QuestionAndAnswerForm.ShowDialog();
                this.Show();
            }
            else if (questionLookupRadioButton.Checked)
            {
                //Gets the 3 things needed for the constructor of the popup box

                string subcategory = (string)lookupSubCategoryComboBox.SelectedItem;
                string professor = (string)lookupProfessorComboBox.SelectedItem;

                this.Hide();
                QuestionsAndAnswers QuestionAndAnswerForm = new QuestionsAndAnswers(professor.Trim(), subcategory.Trim());
                QuestionAndAnswerForm.ShowDialog();
                this.Show();
            }
            else
            {
                MessageBox.Show("Please select a radio button.", "Radio button not selected!");
            }

        }
    }
}
